package com.decent.text.pdf.test2;

import com.decent.text.pdf.test2.service.ApiTestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

import static java.lang.System.in;

/**
 * @author 王雪杰
 * @date 2021/11/13 9:10
 */
@RequestMapping("pdf")
@RestController
public class Api {
    @Resource
    private ApiTestService apiTestService;

    @RequestMapping("allPdf")
    public void  allPdf(HttpServletResponse response) throws Exception {
        apiTestService.createAllPdf(response);
    }

    @RequestMapping("pdfPreviw")
    public void pdfPreviw(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/pdf;charset=UTF-8");
        response.setHeader("Content-Disposition", "inline; filename=print.pdf");
        OutputStream out = null;
        String htmlStr = CreateHtmlByFreemarker.getHtmlStr();
        try {
            out = response.getOutputStream();
            int len = 0;
            byte[] buffer = htmlStr.getBytes();
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }

            out.flush();
            in.close();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
